
package kaizone.haining.chaobohui.widget;

import java.util.List;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.activity.CBHActivity;
import kaizone.haining.chaobohui.activity.DetailActivity;
import kaizone.haining.chaobohui.args.AppArgs;
import kaizone.haining.chaobohui.json.AppRecommend;
import kaizone.haining.chaobohui.json.AppRecommend.LogoUrlModel;
import kaizone.haining.chaobohui.json.Item;
import kaizone.haining.chaobohui.json.Item.PicModel;
import kaizone.haining.chaobohui.util.ImageFetcher;
import kaizone.haining.chaobohui.util.Utils;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

public class FirstReadNearbyView extends LinearLayout {

    public static final int BOTTOM_SPACE = 100;

    private List<Item> mData;

    private MyAdapter mAdapter;

    private ListView mListView;

    private ImageFetcher mImageFetcher;

    public FirstReadNearbyView(Context context) {
        super(context);
        init();
    }

    public FirstReadNearbyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void init() {
        setOrientation(VERTICAL);
        if (getContext() instanceof CBHActivity) {
            mImageFetcher = ((CBHActivity) getContext()).getImageFetcher();
        }
    }

    public void setCultureList(List<Item> data) {
        mData = data;
        loadCulture();
    }

    private void loadCulture() {
        if (mData == null)
            return;
        int count = mData.size();
        for (int i = 0; i < count; i++) {
            final Item item = mData.get(i);
            if (item != null) {
                // FirstReadNearbyCellView nearbyView =
                // (FirstReadNearbyCellView) View.inflate(
                // getContext(), R.layout.item_firstread_nearby, null);
                // nearbyView.setName(item.getName());
                // nearbyView.setIntro(item.getIntro());
                CBHItemView itemView = (CBHItemView) View.inflate(getContext(), R.layout.item_home,
                        null);
                itemView.setTitle(item.getName());
                itemView.setDescription(item.getIntro());
                PicModel picModel = item.getPicModel();
                if (picModel != null) {
                    itemView.loadImage(picModel.url, mImageFetcher);
                }
                itemView.setClickable(true);
                itemView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AppArgs.sNearbyItem = item;
                        DetailActivity.openNearby(getContext());
                    }
                });

                addView(itemView, i);
            }
        }
    }

    public void loadAppRecommends(List<AppRecommend> applist) {
        if (applist == null)
            return;
        int count = applist.size();
        for (int i = 0; i < count; i++) {
            final AppRecommend appRecommend = applist.get(i);
            if (appRecommend != null) {
                // FirstReadNearbyCellView nearbyView =
                // (FirstReadNearbyCellView) View.inflate(
                // getContext(), R.layout.item_firstread_nearby, null);
                // nearbyView.setName(item.getName());
                // nearbyView.setIntro(item.getIntro());
                CBHItemView itemView = (CBHItemView) View.inflate(getContext(), R.layout.item_home,
                        null);
                itemView.setTitle(appRecommend.getAppName());
                itemView.setDescription(appRecommend.getIntro());
                LogoUrlModel picModel = appRecommend.getLogoUrlModel();
                if (picModel != null) {
                    itemView.setImageLayoutParams(80, 80 + 16);
                    itemView.loadImage(picModel.sum100x000, mImageFetcher);
                }
                itemView.setClickable(true);
                itemView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String urlString = appRecommend.getAndroidStoreUrl();
                        Utils.openBrowser(getContext(), urlString);
                    }
                });

                addView(itemView, i);
            }
        }
    }

    private void initListView() {
        mAdapter = new MyAdapter();
        mListView.setAdapter(mAdapter);
        ViewTreeObserver observer = mListView.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            // int layoutCount = 0;

            @Override
            public void onGlobalLayout() {
                if (mData == null)
                    return;
                if (mListView == null)
                    return;
                // ListAdapter adapter = mListView.getAdapter();
                // View child = adapter.getView(0, null, mListView);
                View child = mListView.getChildAt(0);

                int count = mData.size();

                if (child != null && mData != null) {
                    child.measure(0, 0);
                    int height = child.getMeasuredHeight();
                    int totolHeight = height * count + BOTTOM_SPACE;
                    View parent = (View) mListView.getParent();
                    if (parent instanceof LinearLayout) {
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.MATCH_PARENT, totolHeight);
                        mListView.setLayoutParams(params);
                    }
                    // Log.e("onGlobalLayout", "totalheight ="+totolHeight);
                }
            }
        });
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mData != null ? mData.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return mData != null ? mData.get(position) : 0;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            FirstReadNearbyCellView nearbyView = null;
            if (convertView == null) {
                nearbyView = (FirstReadNearbyCellView) View.inflate(getContext(),
                        R.layout.item_firstread_nearby, null);
                convertView = nearbyView;
            }
            nearbyView = (FirstReadNearbyCellView) convertView;
            int size = mData.size();
            if (position < size) {
                Item item = mData.get(position);
                nearbyView.setName(item.getName());
                nearbyView.setIntro(item.getIntro());
                PicModel picModel = item.getPicModel();
                String sum100x000 = picModel.sum100x000;
                // nearbyView.loadImage(mImageFetcher, sum100x000);
            }
            return nearbyView;
        }

    }

}
